<?php
namespace App\Service\Admin;

class TiebaService extends BaseService
{
    //贴吧

    /**
     * 帖子列表
     * @param array $pages【start，limit】
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbTieba)
            ->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //用户
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $uname = '';
            if (!empty($uOne) && $uOne['uname']) {
                $uname = $uOne['uname'];
            }
            //点赞数量
            $like_num = $this->dbConn($this->dbTbLike)
                ->where('tiezi', $v['id'])
                ->count();
            //回复数量
            $reply_num = $this->dbConn($this->dbTbReply)
                ->where('del', 0)
                ->where('tiezi', $v['id'])
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'uname' => $uname,
                'title' => $v['title'],
                'utype_name' => $v['utype']==1 ? '医生' : '患者',
                'like_num' => $like_num,
                'reply_num' => $reply_num,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //详情
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbTieba)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if (!file_exists($res['img'])) {
            $res['img'] = '';
        }
        $res['uname'] = '';
        //用户
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $res['uid'])
            ->first();
        if (!empty($uOne) && $uOne['uname']) {
            $res['uname'] = $uOne['uname'];
        }
        //点赞数量
        $res['like_num'] = $this->dbConn($this->dbTbLike)
            ->where('tiezi', $res['id'])
            ->count();
        //回复数量
        $res['reply_num'] = $this->dbConn($this->dbTbReply)
            ->where('del', 0)
            ->where('tiezi', $res['id'])
            ->count();
        return $this->toback(0, '', $res);
    }

    //评论列表
    public function getReplyList($tiezi)
    {
        $res = $this->dbConn($this->dbTbReply)
            ->where('del', 0)
            ->where('tiezi', $tiezi)
            ->where('pid', 0)
            ->skip(0)->take(10)
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $uOne = (array) $this->dbConn($this->dbUser)->where('id', $v['uid'])->first();
            //点赞数量
            $like_num = $this->dbConn($this->dbTbLike)
                ->where('reply', $v['id'])
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => empty($uOne) ? '-' : $uOne['uname'],
                'cnt' => $v['cnt'],
                'like_num' => $like_num,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }
}